package com.cat.greedyAlgorithm;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximize-happiness-of-selected-children/description/
 * @create 2025/8/21 09:57
 * @since JDK17
 */

public class Solution03 {
    public long maximumHappinessSum(int[] happiness, int k) {
        Arrays.sort(happiness);
        long ans = 0;
        int n = happiness.length - 1;
        for (int i = n, j = 0, t; i > n - k; i--, j++) {
            t = happiness[i] - j;
            ans += t >= 0 ? t : 0;
            if (t == 0) {
                return ans;
            }
        }

        return ans;
    }
}
